<template>
	<view class="myposts-content" v-if="loadEnd">
		<!-- 头部 -->
		<titleBar :show-data="title_data" :status="status" @change="changeIndex"></titleBar>
		<!-- 主体内容 -->
		<view class="myposts-main"><mainList :show-data="dataList" :type="type" @likePosts="likePosts" @collections="collections"></mainList></view>
		<view class="" style="margin: 10rpx 0;color: #656565;font-size: 26rpx;text-align: center;" v-if="dataList.length != 0 && !hasMore">没有更多了~</view>
		<!-- 	<view class="empty" v-if="dataList.length == 0">
			<image src="/static/empty.png" mode="widthFix"></image>
			暂无数据~
		</view> -->
		<view class="safe-padding"></view>
	</view>
</template>

<script>
import ikz from '@/pages/Common/tools.js'

import mainList from '@/pages/Component/forum/component/main-list/mainList.vue'
import titleBar from '@/pages/Component/forum/component/title/title-bar.vue'
export default {
	components: {
		mainList,
		titleBar,
	},
	data() {
		return {
			loadEnd: false,
			type: 'mylike',
			status: '',
			page: 1,
			limit: 5,
			hasMore: false,
			newIndex: 0,
			dataList: [],
			title_data: [
				{
					id: 1,
					name: '收藏',
				},
				{
					id: 2,
					name: '点赞',
				},
				{
					id: 3,
					name: '评论',
				},
			],
		}
	},
	onLoad(option) {
		this.status = option.status
		// console.log('携带过来的status--->',this.status)
		this.loadEnd = false
		if (this.status == 1) {
			this.newIndex = 1
			this.getMyCollection()
		} else if (this.status == 2) {
			this.newIndex = 2
			this.getMyGive()
		} else {
			this.newIndex = 3
			this.getMyComment()
		}
	},
	onPullDownRefresh() {
		this.dataList = []
		this.page = 1
		if (this.newIndex == 1) {
			this.getMyCollection()
		} else if (this.newIndex == 2) {
			this.getMyGive()
		} else if (this.newIndex == 3) {
			this.getMyComment()
		}
	},
	onReachBottom() {
		if (this.hasMore) {
			this.page++
			if (this.newIndex == 1) {
				this.getMyCollection()
			} else if (this.newIndex == 2) {
				this.getMyGive()
			} else if (this.newIndex == 3) {
				this.getMyComment()
			}
		}
	},
	methods: {
		// 切换选项
		changeIndex(id) {
			if (id == this.newIndex) {
				return
			} else {
				this.newIndex = id
				this.dataList = []
				this.page = 1
				if (id == 1) {
					this.getMyCollection()
				} else if (id == 2) {
					this.getMyGive()
				} else {
					this.getMyComment()
				}
			}
		},
		// 获取我的收藏信息
		getMyCollection() {
			let path = '/xyb/posts/posts/user_collection'
			let data = {
				token: uni.getStorageSync('local_user_session'),
				page: this.page,
				limit: this.limit,
			}
			ikz.requestPost(path, data, res => {
				// console.log(res);
				if (res.statusCode == 200) {
					// let circleList = res.data.data.data
					let dataList = []
					dataList = res.data.data.data

					dataList.forEach(el => {
						if (el.images != '') {
							// el.images = el.images.split(',')
							el.imagesNum = el.images.length
							if (el.images.length > 3) {
								el.images.slice(0, 3)
							}
						}
					})
					this.dataList = this.page == 1 ? dataList : [...this.dataList, ...dataList]
					this.hasMore = this.page < res.data.data.count / this.limit ? true : false
				}
				this.loadEnd = true
				uni.stopPullDownRefresh()
			})
		},
		// 获取我的点赞信息
		getMyGive() {
			let path = '/xyb/posts/posts/user_give'
			let data = {
				token: uni.getStorageSync('local_user_session'),
				page: this.page,
				limit: this.limit,
			}
			ikz.requestPost(path, data, res => {
				// console.log(res);
				if (res.statusCode == 200) {
					// let circleList = res.data.data.data
					let dataList = []
					dataList = res.data.data.data

					dataList.forEach(el => {
						if (el.images != '') {
							// el.images = el.images.split(',')
							el.imagesNum = el.images.length
							if (el.images.length > 3) {
								el.images.slice(0, 3)
							}
						}
					})
					this.dataList = this.page == 1 ? dataList : [...this.dataList, ...dataList]
					this.hasMore = this.page < res.data.data.count / this.limit ? true : false
				}
				this.loadEnd = true
				uni.stopPullDownRefresh()
			})
		},
		// 获取我的评论信息
		getMyComment() {
			let path = '/xyb/posts/posts/user_comment'
			let data = {
				token: uni.getStorageSync('local_user_session'),
				page: this.page,
				limit: this.limit,
			}
			ikz.requestPost(path, data, res => {
				// console.log(res);
				if (res.statusCode == 200) {
					// let circleList = res.data.data.data
					let dataList = []
					dataList = res.data.data.data
					dataList.forEach(el => {
						if (el.images != '') {
							// el.images = el.images.split(',')
							el.imagesNum = el.images.length
							if (el.images.length > 3) {
								el.images.slice(0, 3)
							}
						}
					})
					this.dataList = this.page == 1 ? dataList : [...this.dataList, ...dataList]
					this.hasMore = this.page < res.data.data.count / this.limit ? true : false
				}
				this.loadEnd = true
				uni.stopPullDownRefresh()
			})
		},
		// 点赞/取消点赞
		likePosts(id, status) {
			let data = {
				token: uni.getStorageSync('local_user_session'),
				posts_id: id,
			}
			let path = ''
			if (status == 0) {
				path = '/xyb/posts/posts/give'
				ikz.requestPost(path, data, res => {
					if (res.statusCode == 200) {
						this.dataList.forEach(el => {
							if (el.id == id) {
								el.give_status = 1
								el.give++
							}
						})
						uni.showToast({
							title: '点赞成功',
							icon: 'none',
						})
					}
				})
			} else {
				path = '/xyb/posts/posts/give_del'
				ikz.requestPost(path, data, res => {
					if (res.statusCode == 200) {
						this.dataList.forEach(el => {
							if (el.id == id) {
								el.give_status = 0
								el.give--
							}
						})
						uni.showToast({
							title: '取消点赞',
							icon: 'none',
						})
					}
				})
			}
		},
		// 收藏或取消收藏帖子
		collections(id, status) {
			let path = ''
			if (status == 0) {
				path = '/xyb/posts/posts/collection'
			} else {
				path = '/xyb/posts/posts/collection_del'
			}
			let data = {
				token: uni.getStorageSync('local_user_session'),
				posts_id: id,
			}
			ikz.requestPost(path, data, res => {
				if (res.statusCode == 200) {
					let title = ''
					if (status == 0) {
						title = '收藏成功'
						this.dataList.forEach(el => {
							if (el.id == id) {
								el.collection_status = 1
								el.collection++
							}
						})
					} else {
						title = '取消收藏'
						this.dataList.forEach(el => {
							if (el.id == id) {
								el.collection_status = 0
								el.collection--
							}
						})
					}

					uni.showToast({
						title,
						icon: 'none',
					})
				}
			})
		},
	},
}
</script>

<style lang="scss">
page {
	background: #f6f7fb;
}
.myposts-content {
	.myposts-main {
		margin-top: 20rpx;
	}
	.empty {
		display: flex;
		flex-direction: column;
		align-items: center;
		color: #999;
		image {
			margin: 50rpx 0 20rpx;
			width: 40%;
		}
	}
}
</style>
